package org.example.base;

import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

@Getter
@Setter
@ToString
public class JSONResponse {
    /**
     * 定义异常,返回给前端响应的状态码是 200
     * 前端可以通过 success 字段, 判断是显示正确的业务数据 还是 错误码+错误信息
     * 前端:
     * if(响应语句.success){
     *     显示 data字段的数据
     * }else{
     *     显示code错误码+message错误信息
     * }
     */
    private boolean success;
    private String code;
    private String message;
    private Object data;

    public JSONResponse() {
    }

    public static JSONResponse ok(Object o){
        JSONResponse response = new JSONResponse();
        response.success = true;
        response.data = o;
        return  response;
    }

    public static JSONResponse error(){
        return error("ERR000000","未知错误请联系管理员");
    }

    public static JSONResponse error(String code, String message){
        JSONResponse response = new JSONResponse();
        response.code = code;
        response.message=message;
        return response;
    }
}
